package a66weding.com.jiehuntong.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import java.util.HashMap;
import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.beans.FragmentModule;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

/**
 * @version :
 * @creator : AxDu
 * @create-time : 2016/5/29
 * @description :
 */
public abstract class BaseFragmentActivity extends FragmentActivity implements IFragmentCallBack {
    public LinearLayout mBaseHeadView;
    public FrameLayout mBaseFramContent;

    public BaseFragmentActivity() {


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);

        initother(savedInstanceState);

        onPreInitViewExecute();

        FragmentModule defaultModule = getDefaultModule();
        getSupportFragmentManager().beginTransaction().add(getContentId(), defaultModule.getFragment(), defaultModule.getTitle()).addToBackStack(null).commit();

    }


    private void initView() {
        mBaseFramContent = (FrameLayout) findViewById(R.id.flay_baseactivity_content);
        View contentView = getContentView();
        if (null != contentView) {
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT);
            mBaseFramContent.addView(contentView, params);
        }

    }

    public void initother(Bundle savedInstanceState) {
        //处理异常退出恢复情况
        if (savedInstanceState != null) {
            List<Fragment> fragments = getSupportFragmentManager().getFragments();
            if (fragments != null && fragments.size() > 0) {
                boolean showFlag = false;
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                for (int i = fragments.size() - 1; i >= 0; i--) {
                    Fragment fragment = fragments.get(i);
                    if (fragment != null) {
                        if (!showFlag) {
                            ft.show(fragments.get(i));
                            showFlag = true;
                        } else {
                            ft.hide(fragments.get(i));
                        }
                    }
                }
                ft.commit();
            }
        }
    }

    /**
     * 在视图初始化之前执行的方法, 由子类选择实现
     */
    protected void onPreInitViewExecute() {

    }


    /**
     * 获取顶部状态栏view
     * @return
     */
    /**
     * 获取内容视图, 由子类返回对应的布局视图
     */
    protected abstract View getContentView();


    /**
     * 在视图初始化之后执行的方法, 由子类选择实现
     */
    protected void onPostCreateExecute() {

    }

    @Override
    public void home() {
        while (getSupportFragmentManager().getBackStackEntryCount() != 1) {
            getSupportFragmentManager().popBackStackImmediate();
        }
    }


    @Override
    public void jump(String tag, BaseFragment current) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        HashMap<String, FragmentModule> modules = getFragmentModule();
        BaseFragment target = (BaseFragment) getSupportFragmentManager().findFragmentByTag(tag);

        target = modules.get(tag).getFragment();
        transaction.hide(current)
                .add(getContentId(), target, tag)
                .addToBackStack(null)
                .commit();


        setTitle(modules.get(tag).getTitle());
    }

    protected abstract FragmentModule getDefaultModule();

    protected abstract int getContentId();

    protected abstract HashMap<String, FragmentModule> getFragmentModule();


}
